package com.vehicle.car.controller;

import java.util.HashMap;
import java.util.List;
import java.io.IOException;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import com.vehicle.car.perpetual.MyPerpetual;
import com.vehicle.car.utils.HttpUtils;
import com.vehicle.car.utils.ImageToBase64Utils;
import com.vehicle.car.utils.MD5Utils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.vehicle.common.log.annotation.Log;
import com.vehicle.common.log.enums.BusinessType;
import com.vehicle.common.security.annotation.PreAuthorize;
import com.vehicle.car.domain.DriverLicenseInfo;
import com.vehicle.car.service.IDriverLicenseInfoService;
import com.vehicle.common.core.web.controller.BaseController;
import com.vehicle.common.core.web.domain.AjaxResult;
import com.vehicle.common.core.utils.poi.ExcelUtil;
import com.vehicle.common.core.web.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 驾驶证Controller
 *
 * @author XU
 * @date 2025-03-30
 */
@RestController
@RequestMapping("/car")
@Slf4j
public class DriverLicenseInfoController extends BaseController
{
    @Autowired
    private IDriverLicenseInfoService driverLicenseInfoService;


    /**
     * 查询驾驶证列表
     */
    @PreAuthorize(hasPermi = "car:car:list")
    @GetMapping("/list")
    public TableDataInfo list(DriverLicenseInfo driverLicenseInfo)
    {
        startPage();
        List<DriverLicenseInfo> list = driverLicenseInfoService.selectDriverLicenseInfoList(driverLicenseInfo);
        return getDataTable(list);
    }

    /**
     * 导出驾驶证列表
     */
    @PreAuthorize(hasPermi = "car:car:export")
    @Log(title = "驾驶证", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DriverLicenseInfo driverLicenseInfo) throws IOException
    {
        List<DriverLicenseInfo> list = driverLicenseInfoService.selectDriverLicenseInfoList(driverLicenseInfo);
        ExcelUtil<DriverLicenseInfo> util = new ExcelUtil<DriverLicenseInfo>(DriverLicenseInfo.class);
        util.exportExcel(response, list, "驾驶证数据");
    }

    /**
     * 获取驾驶证详细信息
     */
    @PreAuthorize(hasPermi = "car:car:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(driverLicenseInfoService.selectDriverLicenseInfoById(id));
    }

    /**
     * 新增驾驶证
     */
    @PreAuthorize(hasPermi = "car:car:add")
    @Log(title = "驾驶证", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody DriverLicenseInfo driverLicenseInfo)
    {
        return toAjax(driverLicenseInfoService.insertDriverLicenseInfo(driverLicenseInfo));
    }

    /**
     * 修改驾驶证
     */
    @PreAuthorize(hasPermi = "car:car:edit")
    @Log(title = "驾驶证", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody DriverLicenseInfo driverLicenseInfo)
    {
        return toAjax(driverLicenseInfoService.updateDriverLicenseInfo(driverLicenseInfo));
    }

    /**
     * 删除驾驶证
     */
    @PreAuthorize(hasPermi = "car:car:remove")
    @Log(title = "驾驶证", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(driverLicenseInfoService.deleteDriverLicenseInfoByIds(ids));
    }

    /**
     * 驾驶证ocr验证并保存
     * @param file
     * @return
     */
    @PostMapping("/drivingLicence")
    public AjaxResult drivingLicenceOCR(@RequestParam("file") MultipartFile file) {
        JSONObject jsonObject = driverLicenseInfoService.drivingLicence(file);
        System.out.println(jsonObject);
        return AjaxResult.success(jsonObject);
    }

}
